/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.sdkj.dspservice.common.base.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.sdkj.dspservice.common.utils.StringUtils;
import com.sdkj.dspservice.modules.sys.entity.User;
import lombok.Data;

import java.util.Date;

/**
 * 数据Entity类
 * @author ThinkGem
 * @version 2014-05-16
 */
@JsonInclude
@Data
public abstract class DataEntity<T> extends BaseEntity<T> {

	private static final long serialVersionUID = 1L;

	/**
	 * 备注
	 */
	protected String remarks;
	/**
	 * 创建者
	 */
	@TableField(fill = FieldFill.INSERT)
	protected String createBy;
	/**
	 * 创建日期
	 */
	@TableField(fill = FieldFill.INSERT)
	protected Date createDate;
	/**
	 * 更新者
	 */
	@TableField(fill = FieldFill.INSERT_UPDATE)
	protected String updateBy;
	/**
	 * 更新日期
	 */
	@TableField(fill = FieldFill.INSERT_UPDATE)
	protected Date updateDate;
	/**
	 * 删除标记（0：正常；1：删除；2：审核）
	 */
	@TableField(fill = FieldFill.INSERT)
	protected String deleteFlag;

	/**
	 * 以下用于参数传递方便
	 */
	@TableField(exist = false)
	protected String createDateSearch;
	@TableField(exist = false)
	protected User createByUser;
	@TableField(exist = false)
	protected User updateByUser;

	public DataEntity() {
		super();
	}
	
	public DataEntity(String id) {
		super(id);
	}

	/**
	 * 把实体里的参数，放入查询中（只有关联查询需要这样，因为设置不了别名，只能手动插入）
	 * @return
	 */
	public QueryWrapper<T> makeQueryWrapper(QueryWrapper<T> queryWrapper){
		if(createByUser != null && StringUtils.isNotBlank(createByUser.getName())){
			queryWrapper.like("cu.name", createByUser.getName());
		}
		if(StringUtils.isNotBlank(createDateSearch)){
			String[] dates = createDateSearch.split(",");
			queryWrapper.apply("a.create_date >= '" + dates[0] + "'");
			queryWrapper.apply("a.create_date <= '" + dates[1] + "'");
		}
		return queryWrapper;
	}
	
}
